<template>
  <div class="page-department">
    <div class="component-mainHeader">
      <div class="top">
        <div class="topleft">
          <p>
            <span class="iconfont" style="color:#1abc9a;margin-right:3px;">&#xe607;</span>网格管理
          </p>
        </div>
        <div class="topright">
          <el-button type="info" @click="$router.go(0)">
            <span style="font-weight:700;margin-right:5px;" class="iconfont">&#xe662;</span>刷新
          </el-button>
        </div>
      </div>
    </div>
                          <!-- AreaManager -->
    <div v-if="ms_roleName == 'ProjectClerk'">
      <div class="component-searchHeader">
        <div class="search-header">
          <div class="left-title">
            <p class="left-text">
              <span class="iconfont">&#xe63d;</span> 筛选查询
            </p>
            <div class="right">
              <button @click="findRes">查询</button>
            </div>
          </div>
        </div>
      </div>

      <div class="component-search-box">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="四级地址:">
            <el-select v-model="formInline.fourthAddr" placeholder="选择四级地址" clearable @change="findFifthAddr">
              <el-option
                v-for="item in fourthAddr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="五级地址:">
            <el-select v-model="formInline.fifthAddr" placeholder="选择五级地址" clearable>
              <el-option
                v-for="item in fifthAddr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="施工队:">
            <el-select v-model="formInline.orgId" placeholder="选择施工队" clearable>
              <el-option
                v-for="item in orgs"
                :key="item.orgId"
                :label="item.orgName"
                :value="item.orgId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <div class="component-listHeader">
        <div class="search-header">
          <div class="left-title">
            <p class="left-text">
              <span class="iconfont">&#xe61e;</span>
              网格管理
            </p>
            <div class="right">
              <button @click="$router.push({name:'addrManage'})" style="width:100px;">地址管理</button>
            </div>
          </div>
        </div>
      </div>

      <div class="table">
        <el-table
          ref="multipleTable"
          :data="info"
          tooltip-effect="dark"
          style="width:100%"
          border
          @selection-change="handleSelectionChange"
          :row-key="getRowKey"
        >
          <el-table-column align="center" :reserve-selection="true" width="50" type="selection"></el-table-column>
          <el-table-column label="一级地址" align="center" width="155" prop="firstLevel"></el-table-column>
          <el-table-column label="二级地址" align="center" width="155" prop="secondLevel"></el-table-column>
          <el-table-column label="三级地址" align="center" width="155" prop="thirdLevel"></el-table-column>
          <!-- <el-table-column label="四级地址" align="center" width="155" prop="fourthLevel"></el-table-column>
          <el-table-column label="五级地址" align="center" width="155" prop="fifthLevel"></el-table-column> -->
          <el-table-column label="施工队" align="center" width="155" prop="orgNames"></el-table-column>
          <el-table-column label="操作信息" align="center">
            <template slot-scope="scope">
              <div>
                <a class="options" @click.prevent="handleOrg(scope.row,1)">绑定施工队</a>
              </div>
              <div>
                <a
                  class="options"
                  @click.prevent="$router.push({name:'includeOrg',params:{orgs:scope.row.orgs,fifthId:scope.row.fifthId}})"
                >编辑</a>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="component-paging" v-if="info.length > 0">
        <div class="table-bottom">
          <div class="bottom-left" style="margin-left: 42px;">
            <button @click="handleOrg" style="width:130px;">批量绑定施工队</button>
          </div>
          <div class="paging">
            <div class="paging-left">
              <p>
                共
                <span>{{Math.ceil(total/pageSize)}}</span> 页/
                <span>{{total}}</span> 条数据
              </p>
            </div>
            <el-pagination
              background
              @size-change="SizeChange($event,dataInit,currentPage,pageSize)"
              @current-change="CurrentChange($event,dataInit,currentPage,pageSize)"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30]"
              :page-size="pageSize"
              layout="sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </div>
      </div>
    </div>
                          <!-- AreaManager -->
    <div v-if="ms_roleName != 'ProjectClerk'">
      <div class="component-searchHeader">
        <div class="search-header">
          <div class="left-title">
            <p class="left-text">
              <span class="iconfont">&#xe63d;</span> 筛选查询
            </p>
            <div class="right">
              <button @click="findRes">查询</button>
            </div>
          </div>
        </div>
      </div>
      <div class="component-search-box">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="四级地址:">
            <el-select v-model="formInline.fourthAddr" placeholder="选择四级地址" clearable @change="findFifthAddr">
              <el-option
                v-for="item in fourthAddr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="五级地址:">
            <el-select v-model="formInline.fifthAddr" placeholder="选择五级地址" clearable>
              <el-option
                v-for="item in fifthAddr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="component-listHeader">
        <div class="search-header">
          <div class="left-title">
            <p class="left-text">
              <span class="iconfont">&#xe61e;</span>
              网格管理
            </p>
            <div class="right">
              <button @click="$router.push({name:'addrManage'})" style="width:100px;">地址管理</button>
            </div>
          </div>
        </div>
      </div>
      <div class="table">
        <el-table ref="multipleTable" :data="info" tooltip-effect="dark" style="width:100%" border>
          <el-table-column label="一级地址" align="center" width="190" prop="firstLevel"></el-table-column>
          <el-table-column label="二级地址" align="center" width="190" prop="secondLevel"></el-table-column>
          <el-table-column label="三级地址" align="center" width="190" prop="thirdLevel"></el-table-column>
          <el-table-column label="四级地址" align="center" width="190" prop="fourthLevel"></el-table-column>
          <el-table-column label="五级地址" align="center" prop="fifthLevel"></el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="component-paging" v-if="info.length > 0">
        <div class="table-bottom">
          <div class="bottom-left" style="margin-left: 42px;"></div>
          <div class="paging">
            <div class="paging-left">
              <p>
                共
                <span>{{Math.ceil(total/pageSize)}}</span> 页/
                <span>{{total}}</span> 条数据
              </p>
            </div>
            <el-pagination
              background
              @size-change="SizeChange($event,dataInit,currentPage,pageSize)"
              @current-change="CurrentChange($event,dataInit,currentPage,pageSize)"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30]"
              :page-size="pageSize"
              layout="sizes, prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
          </div>
        </div>
      </div>
    </div>
    <!--施工队弹框-->
    <div class="component-models">
      <el-dialog title="绑定施工队" :visible.sync="dialog.flag" width="660px">
        <el-form :model="dialog" ref="changeOrder" label-width="200px" class="demo-ruleForm">
          <el-form-item label="施工队:">
            <el-select
              v-model="dialog.type"
              placeholder="请选择施工队"
              style="width:217px;"
              @change="changeManage"
            >
              <el-option
                v-for="item in dialogOrgs"
                :key="item.orgId"
                :label="item.orgName"
                :value="item.orgId"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="项目经理:">
            <span style="float: left;">{{}}</span>
          </el-form-item>-->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog.flag = false">取消</el-button>
          <el-button @click="submitBind">绑定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "operation",
  data() {
    return {
      ms_roleName: localStorage.getItem("roleName"),
      // 表格数据
      info: [{}],
      // 条件查询
      formInline: {
        fourthAddr: "",
        fifthAddr: "",
        orgId: ""
      },
      //  四级地址下拉框
      fourthAddr: [],
      // 五级地址下拉框
      fifthAddr: [],
      // 条件查询施工队
      orgs:[],
      // 数据总条数
      total: 0,
      // 每页显示数据的条数
      pageSize: 10,
      // 当前显示的页面页码
      currentPage: 1,
      dialog: {
        flag: false
      },
      // 弹窗下拉框
      dialogOrgs:[],
      // 全选列表
      multipleSelection: [],
      // 单选五级id
      chooseFifthId:[],
    };
  },
  created() {
    // 表格数据
    this.dataInit();
    // 四级地址-五级地址-项目部信息 3个下拉框选项查询
    //this.selectInit();
  },
  mounted() {},
  methods: {
    // 四级地址-五级地址-项目部信息 3个下拉框选项查询
    // selectInit() {
    //   var that = this;
    //   let suCb = function(res) {
    //       console.log("下拉框信息", res.data);
    //     },
    //     erCb = function(res) {},
    //     palyParam = {
    //       url: "/api/web/",
    //       methods: "post",
    //       data: "",
    //       success: suCb,
    //       error: erCb
    //     };
    //   this.$https(palyParam);
    // },
    // 表格数据
    dataInit() {
      let that = this,
        suCb = function(res) {
          console.log("表格查询结果", res);
          that.total = res.data.data.rowCount;
          // that.info = that.deepClone(res.data.data.options.dataList);
          that.info = that.deepClone(res.data.data.page);
          that.orgs = that.deepClone(res.data.data.orgs);
          that.dialogOrgs = that.deepClone(res.data.data.orgs);
          that.fourthAddr = that.deepClone(res.data.data.tree);
          // 取出所属单位数组中的name用,拼接
          that.info.forEach(item => {
            item.orgNames = item.orgs.map( el => {
              return el.orgName;
            }).join(',');
          })
          console.log(that.info);
        },
        erCb = function(res) {

        },
        palyParam = {
          url: "/api/web/SysAddressManagement/selectListAddress",
          methods: "post",
          data: {},
          success: suCb,
          error: erCb
        };
        if(that.ms_roleName == 'AreaManager'){
          palyParam.data = {
            orgId: that.formInline.orgId,
            fifthId: that.formInline.fifthAddr,
            fourthId: that.formInline.fourthAddr,
            pageCurrent: that.currentPage,
            pageSize: that.pageSize
          }
        }else{
          palyParam.data = {
            fifthId: that.formInline.fifthAddr,
            fourthId: that.formInline.fourthAddr,
            pageCurrent: that.currentPage,
            pageSize: that.pageSize
          }
        }
      console.log("查询参数", palyParam.data);
      that.$https(palyParam);
    },
    // 查询按钮
    findRes() {
      this.currentPage = 1;
      this.dataInit();
    },
    // 点击弹出下拉框
    handleOrg(row,type) {
      this.dialog.flag = !this.dialog.flag;
      // 如果点击单个，选的id为[row.fifthId]
      if(type === 1){
        this.chooseFifthId = [row.fifthId];
      }else{
        this.chooseFifthId = this.multipleSelection;
      }
    },
    // 项目下拉框change绑定函数
    changeManage() {},
    // 绑定按钮
    submitBind() {
      console.log(this.dialog.type)
        if(this.dialog.type){
          let that = this,
        suCb = function(res) {
          console.log("绑定结果", res);
          that.$msg('绑定成功')
          that.dialog.flag = false;
          that.dialog.type = "";
          that.dataInit();
        },
        erCb = function(res) {},
        postData = {
          positionIds: that.chooseFifthId,
          orgId: that.dialog.type
        },
        palyParam = {
          url: "/api/web/sysOrg/bindWorkCompany",
          methods: "post",
          data: postData,
          success: suCb,
          error: erCb
        };
      console.log("查询参数", postData);
      that.$https(palyParam);
        }else{
          this.$erMsg('请选择绑定的施工队')
        }
    },
    //全选
    handleSelectionChange(val) {
      let arr = [];
      if (val.length > 0) {
        val.forEach(item => {
          arr.push(item.fifthId);
          this.multipleSelection = arr;
        });
      } else {
        this.multipleSelection = [];
      }
      console.log(this.multipleSelection);

      //   let storageExpansion = localStorage.getItem('checkedExpansion')
      //   storageExpansion = storageExpansion.push(this.multipleSelection)
      //   localStorage.setItem('checkedExpansion',storageExpansion)
    },
    // 记录全选后的key值
    getRowKey(row) {
      return row.fifthId;
    },
    // 五级地址下拉框获取数据
    findFifthAddr(val){
      this.formInline.fifthAddr = '';
      if(val){
        this.fifthAddr = this.fourthAddr.filter(
        item => {
          if(item.value == val){
            return item
          }
        }
      )[0].children;
      }else{
        this.fifthAddr = [];
      }
      console.log(this.fifthAddr)
    }
  }
};
</script>

<style scoped>
.component-searchHeader .search-header {
  width: 1100px;
  box-sizing: border-box;
  border: 1px solid rgba(228, 228, 228, 1);
  background-color: rgba(255, 255, 255, 1);
  margin: 20px auto 0;
}

.component-searchHeader .search-header .left-title {
  display: flex;
  justify-content: space-between;
  width: 100%;
  background-color: rgba(243, 243, 243, 1);
  height: 45px;
  box-sizing: border-box;
}

.component-searchHeader .search-header .left-title .left-text {
  width: 20%;
  color: #666;
  line-height: 45px;
  font-family: "微软雅黑";
  font-weight: 400;
  font-size: 12px;
  text-align: left;
  margin: 0 0 0 30px;
}

.component-searchHeader .right {
  width: 70%;
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}

.component-searchHeader .right button {
  padding: 0 15px;
  height: 30px;
  margin: 6px 5px;
  line-height: 30px;
  font-size: 12px;
  color: #666;
  background-color: #fff;
  border: 1px solid #ccc;
}

.component-searchHeader .right button:hover {
  background-color: #f3f3f3;
}

.component-search-box {
  width: 1100px;
  height: 60px;
  box-sizing: border-box;
  background-color: #fff;
  border: 1px solid #e4e4e4;
  border-top: none;
  margin: 0 auto;
  text-align: left;
  padding-left: 30px;
  line-height: 60px;
}

.component-search-box .el-input__inner {
  height: 36px;
  display: inline-block;
  vertical-align: middle;
}

.component-listHeader .search-header {
  width: 1100px;
  box-sizing: border-box;
  border: 1px solid rgba(228, 228, 228, 1);
  background-color: rgba(255, 255, 255, 1);
  margin: 20px auto 0;
  border-bottom: 0;
}

.component-listHeader .search-header .left-title {
  display: flex;
  justify-content: space-between;
  width: 100%;
  background-color: rgba(243, 243, 243, 1);
  height: 45px;
  box-sizing: border-box;
}

.component-listHeader .search-header .left-title .left-text {
  width: 20%;
  color: #666;
  line-height: 45px;
  font-family: "微软雅黑";
  font-weight: 400;
  font-size: 12px;
  text-align: left;
  margin: 0 0 0 30px;
}

.component-listHeader .search-header .right {
  width: 70%;
  display: flex;
  justify-content: flex-end;
  margin-right: 10px;
}

.component-listHeader .search-header .right button {
  padding: 0 15px;
  height: 30px;
  width: 80px;
  margin: 6px 5px;
  line-height: 30px;
  font-size: 14px;
  color: #666;
  background-color: #fff;
  border: 1px solid #ccc;
}

.component-listHeader .search-header .right button:hover {
  background-color: #f3f3f3;
}

.component-listHeader .search-header .right .right-select-margin {
  margin-top: 6px;
  margin-right: 8px;
}

.component-table-header {
  width: 1100px;
  margin: 0 auto;
  display: flex;
  border: 1px solid #e4e4e4;
  background-color: #f9fafc;
  height: 45px;
  line-height: 45px;
  padding: 0;
  list-style: none;
  justify-content: space-between;
  box-sizing: border-box;
  border-top: none;
}

.component-table-header li:first-child {
  width: 50px;
}

.component-table-header li:first-child span {
  border-radius: 5px;
  border: 2px solid #aba2a2;
  display: inline-block;
  width: 16px;
  height: 16px;
  margin: 12.5px auto;
  line-height: 16px;
  font-size: 16px;
}

.component-table-header li {
  border-right: 1px solid #e4e4e4;
  font-family: "PingFangSC-Semibold", "PingFang SC Semibold", "PingFang SC";
  font-weight: 650;
  font-style: normal;
  font-size: 14px;
  color: #666666;
}
.component-table-header li:last-child {
  border: none;
}

.component-table-body {
  width: 1100px;
  margin: 0 auto;
  display: flex;
  border: 1px solid #e4e4e4;
  background-color: #fff;
  height: 45px;
  line-height: 45px;
  padding: 0;
  list-style: none;
  justify-content: space-between;
  box-sizing: border-box;
  border-top: none;
}

.component-table-body li {
  font-size: 12px;
  color: #666;
  text-align: center;
  border-right: 1px solid #e4e4e4;
}

.component-table-body li:first-child {
  width: 50px;
}

.component-table-body li:nth-child(2) {
  width: 100px;
}

.component-table-body li:nth-child(3) {
  width: 150px;
}

.component-table-body li:nth-child(4) {
  width: 150px;
}
.component-table-body li:nth-child(5) {
  width: 150px;
}

.component-table-body li:last-child {
  width: 500px;
  border-right: none;
}

.component-table-body li:last-child {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  padding: 0 auto;
}

.component-table-body li:last-child a {
  display: inline-block;
  text-align: center;
  color: #1abc9c;
  text-decoration: none;
  font-size: 12px;
}

/* 下面都是修改饿了么ui的下拉框和按钮选中后的样式 */

.el-select .el-input.is-focus .el-input__inner,
.el-select .el-input__inner:focus {
  border-color: #e4e4e4;
}

.el-select-dropdown__item.selected {
  color: #666;
  font-weight: 400;
}

.el-select-dropdown {
  min-width: 100px !important;
  top: 50px !important;
  margin-left: 5px;
}
.el-select-dropdown__item {
  height: 26px;
  line-height: 26px;
}

.el-select .el-input__inner {
  width: 100px;
  padding: 0 8px;
  margin: 6px 5px;
}
.component-paging .table-bottom .all-left button {
  background-color: #fff;
}
.component-paging .table-bottom .all-left button:hover {
  background-color: #f3f3f3;
}

.table {
  width: 1100px;
  margin: 0 auto;
}
.options {
  color: #1abc9c;
  padding: 0 5px;
  cursor: pointer;
}
.el-textarea {
  width: 220px;
  vertical-align: top;
}

.component-models .el-form-item {
  width: 512px;
  margin-bottom: 10px;
}
.component-models .el-select {
  left: -50px;
}
</style>
